//
//  ViewController.m
//  Net
//
//  Created by Justsoso on 16/3/24.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>

{
    NSMutableData *mData;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    
    NSLog(@"协议:%@",url.scheme);
    NSLog(@"主机:%@",url.host);
    NSLog(@"路径:%@",url.relativePath);
    NSLog(@"路径:%@",url.path);
    NSLog(@"端口:%@",url.port);
    NSLog(@"请求:%@",url.query);
}
- (IBAction)btnPressed:(id)sender
{
    // 1. 网址
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    
    // 2. 请求dauix
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 3. 发请求(同步请求,等待服务器返回数据回来后继续向下运行)
    
    NSURLResponse *response;
    NSError *error;
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSHTTPURLResponse *httpResponse = response;
    NSLog(@"状态码 %i",httpResponse.statusCode);
    NSLog(@"响应头 %@",httpResponse.allHeaderFields);
    
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@",str);
}

- (IBAction)btn2Pressed:(id)sender
{
    // 1. 网址
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/MJServer/login?username=123&pwd=123&method=get&type=JSON"];
    
    // 2. 请求dauix
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 3. 发请求(同步请求,等待服务器返回数据回来后继续向下运行)
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",str);
    }];
    
}
- (IBAction)btn3Pressed:(id)sender
{
    // 1. 网址
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_02.png"];
    
    // 2. 请求dauix
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 发出请求
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    mData = [NSMutableData data];
    [connection start];
}

- (IBAction)btn4Pressed:(id)sender
{
    // 1. 网址
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login"];
    
    // 2. 请求dauix
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    
    NSData *postData = [@"username=123&pwd=123&method=get&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = postData;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",str);
    }];
    
    
    
//    // 1. 网址 POST
//    NSURL *url = [NSURL URLWithString:@"http://192.168.13.28:8080/MJServer/login"];
//    
//    //?username=123&pwd=123&method=get&type=JSON
//    
//    // 2. 请求对象
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    request.HTTPMethod = @"POST";// 默认GET请求，指定为POST
//    
//    //字符串转换成数据放在请求体中
//    NSData *postData = [@"username=123&pwd=123&method=get&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];//NSString -> NSData
//    request.HTTPBody = postData;
//    
//    // 3. 发请求 (异步请求,不会卡主线程,在后台请求数据，数据回来后，执行block里的处理方法)
//    NSDate *d1 = [NSDate date];
//    
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        // 数据 -> 文字
//        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        // 显示结果
//        NSLog(@"%@",str);
}

#pragma mark - NSURLConnectionData Delegate Method


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
    NSHTTPURLResponse *h = response;
    NSLog(@"接收到服务器响应 %i %@",h.statusCode,h.allHeaderFields);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"接受到服务器返回的数据%i",data.length);
    [mData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"接受完成,显示到界面");
    UIImage *image = [UIImage imageWithData:mData];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
    imageView.frame = CGRectMake(100, 300, image.size.width, image.size.height);
}

@end
